/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.stdlib.core.type;

import axil.api.AxilObject;
import axil.api.AxilType;
import axil.api.Formatting;
import axil.api.Persona;
import axil.framework.type.Concatenated;
import axil.framework.type.Mathematical;
import axil.framework.type.Sized;
import axil.stdlib.collection.type.Keyed;
import axil.stdlib.collection.type.Sequence;
import axil.stdlib.numbers.type.Int;
import axil.stdlib.numbers.type.Num;

import java.math.RoundingMode;
import java.util.Iterator;


/**
 * The Axil concept of nothingness. This is like Java null, only actually
 * useful. :)  Axil provides a nil object akin to Smalltalk or Lisp.  C. A. R.
 * Hoare calls null his "billion dollar mistake -- if you don't know why, then
 * you shouldn't be writing code.
 */
public class Nil extends Atom implements Iterable<AxilObject>,
	Sequence<AxilObject>, Sized, Mathematical, Keyed, Concatenated {
	public static final AxilType type = Nil_Type.object;
	public static final Nil object = new Nil();
	public static final String text = "(nil)";


	private Nil() {
	}


	/**
	 * Get the type metadata for this object. The type metadata contains
	 * information about the type itself, as an object.
	 */
	public AxilType type() {
		return type;
	}


	/**
	 * Tell if this object, represents a true or false value. In general, true
	 * is returned for any object that is not equivalent to nil, is a number
	 * greater than zero, or is the boolean true value.
	 */
	public boolean bool() {
		return false;
	}


	/**
	 * Return the host application (or built-in Java) object that most closely
	 * matches this value object. Return returned value is never null unless
	 * this value object represents the special 'nil' object.
	 */
	public Object intrinsic() {
		return null;
	}


	/**
	 * Returns a hash code value for the object. All Axil values must provide a
	 * meaningful implementation of this function to enable themselves to be
	 * placed in a map or set.
	 */
	protected int hash() {
		return 0;
	}


	/**
	 * Get the size of this object. The value is the number of contained objects,
	 * not the number of bytes consumed by the object. The size can be any value
	 * greater than or equal to zero.
	 */
	public int size() {
		return 0;
	}


	/**
	 * Tell if this object is equivalent to the given object. The object given
	 * is never null. The object given may be of any type of value object.  If
	 * the given object is not a suitable type for comparison, a
	 * ClassCastException may be thrown.
	 */
	public boolean equalTo(AxilObject object) {
		return this == object;
	}


	/**
	 * Compares this object with the specified object for order. If the given
	 * object is not a suitable type for comparison, a ClassCastException may
	 * be thrown.
	 *
	 * @param object
	 * 	The object to compare against. The given object cannot be null but may
	 * 	be any Axil object.
	 *
	 * @return
	 * 	Returns a negative integer, zero, or a positive integer as this object
	 * 	is less than, equal to, or greater than the specified object.
	 */
	public int comparedTo(AxilObject object) {
        // "nil" comparison; nil is equal to nil.
        if (this == object) {
            return 0;
        }
        return -1;
	}


	/**
	 * Return the canonical representation of a zero value in this numeric
	 * type.
	 */
	public Num zero() {
		return Int.zero;
	}


	/**
	 * Concatenate the given object onto this object, returning a new object.
	 * Neither this object nor the given object are altered. The given object
	 * is never null or nil, but may not be an object of the expected type.
	 */
	public AxilObject concatenate(AxilObject other)	{
		return this;
	}


	/**
	 * Add the given value to this value, returning a new object. The given
	 * value is never null or nil. The value may be a numeric type of any other
	 * type of object that can sensibly implement the Added interface.
	 */
	public AxilObject add(AxilObject object) {
		return this;
	}


	/**
	 * Subtract the given object from this object. The given value is never
	 * null or nil. The value may be a numeric type of any other type of object
	 * that can sensibly implement the Subtracted interface.
	 */
	public AxilObject subtract(AxilObject n) {
		return this;
	}


	/**
	 * Multiply the given number by this number. This method can safely assume
	 * that the given value is not null or nil, but can be any of the numeric
	 * types.
	 */
	public Num multiply(Num n) {
		return n.zero();
	}


	/**
	 * Divide this number by the given number. This method can safely assume
	 * that the given value is not null or nil, but can be any of the numeric
	 * types.
	 */
	public Num divide(Num n) {
		return n.zero();
	}


	/**
	 * Round this value according to the rules of the given rounding mode and
	 * to the given number of significant fractional digits. A value of zero
	 * rounds this to a whole number.
	 */
	public Num round(RoundingMode mode, int digits) {
		return Int.zero;
	}


	/**
	 * Return the key where the given object resides within this collection.
	 * A nil value is returned if the object does not live in this collection.
	 */
	public AxilObject keyof(AxilObject object) {
		return this;
	}


	/**
	 * Return the object at the given key, or nil if the key is not contained
	 * within this object.
	 */
	public AxilObject at(AxilObject key) {
		return this;
	}


	/**
	 * Get the first element from this sequential collection, or return null
	 * if this collection is empty.
	 */
	public AxilObject first() {
		return this;
	}


	/**
	 * Get the last element from this sequential collection, or return null if
	 * this collection is empty.
	 */
	public AxilObject last() {
		return this;
	}


	/**
	 * Get the element at the given index, where the first object is at index
	 * zero. If there is no element at that position, then a null is returned.
	 */
	public AxilObject at(int index) {
		return this;
	}


	private static final class NilIterator implements Iterator<AxilObject> {
		public boolean hasNext()	{
			return false;
		}
		public AxilObject next()	{
			return null;
		}
		public void remove() {
			throw new UnsupportedOperationException(
			    "Objects cannot be removed from this collection.");
		}
	}


	/**
	 * Returns an iterator over a set of elements of type T.
	 *
	 * @return an Iterator.
	 */
	public Iterator<AxilObject> iterator() {
		return new NilIterator();
	}


	/**
	 * Format this object in a locale-friendly manner. The string returned is
	 * a localized representation of this object using the locale and timezone
	 * associated with this persona. The formatting given is never null and
	 * matches formatting suitable for this type.
	 */
	public String format(Persona persona, Formatting formatting) {
		return "";
	}


	/**
	 * Return a string representation of this object. The string returned is
	 * never null. For a nil value, we return a blank string.
	 */
	public String toString() {
		return "";
	}
}
